package guoanmaker.operator.business.service;

import java.io.BufferedInputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import guoanmaker.operator.business.dao.OperatorContractDao;
import guoanmaker.operator.business.dao.OperatorContractDemandDao;
import guoanmaker.operator.business.model.OperatorContract;
import guoanmaker.operator.business.model.OperatorContractDemand;
import guoanmaker.operator.business.model.OperatorContractDownloadCount;
import guoanmaker.operator.business.model.OperatorContractDownloadLog;
import guoanmaker.operator.business.model.OperatorContractType;
import guoanmaker.operator.business.model.OperatorMailConfig;
import guoanmaker.operator.business.model.PersonalUser;
import guoanmaker.operator.business.modelnobind.OperatorContractBind;
import guoanmaker.operator.business.modelnobind.OperatorContractDemandBind;
import guoanmaker.operator.business.repository.OperatorApkPathRepository;
import guoanmaker.operator.business.repository.OperatorContractDemandRepository;
import guoanmaker.operator.business.repository.OperatorContractDownloadCountRepository;
import guoanmaker.operator.business.repository.OperatorContractDownloadLogRepository;
import guoanmaker.operator.business.repository.OperatorContractRepository;
import guoanmaker.operator.business.repository.OperatorContractTypeRepository;
import guoanmaker.operator.business.repository.OperatorMailConfigRepository;
import guoanmaker.operator.business.repository.PersonalUserRepository;

/**
 * 
* @ClassName: OperatorContractService 
* @Description: TODO(合同操作类) 
* @author (ZhangXinYu)  
* @date 2017年12月17日 上午11:20:58 
* @version V1.0
 */
@Service
public class OperatorApkService implements OperatorApkServiceInterface {

	@Autowired
	private OperatorApkPathRepository apkPathRepository;

	/**
	 * 下载文件
	 */
	@Transactional
	@Override
	public void downloadFile(HttpServletResponse response){
			try{
				String fileName = "国安创客";
				String urlstr = apkPathRepository.findAll().get(0).getUrl();
				//通过截取url获取文件类型
				fileName += urlstr.substring(0,urlstr.indexOf('?')).substring(urlstr.substring(0,urlstr.indexOf('?')).lastIndexOf("."));
				URL url = null;
				url = new URL(urlstr);
				response.reset();  
				response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'"+URLEncoder.encode(fileName, "UTF-8"));
				response.setHeader("Connection", "close");  
				response.setHeader("Content-Type", "application/octet-stream");
				HttpURLConnection connection = (HttpURLConnection)url.openConnection();
				BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
				OutputStream out = response.getOutputStream();
				int len = -1;
				byte[] b = new byte[1024*1024];
				while((len = is.read(b)) != -1 ){
					out.write(b, 0, len);
				}
				out.flush();
				out.close();
				is.close();
				
			}catch (Exception e) {
				e.printStackTrace();
			}
	}

}
